# -*- coding: utf-8 -*-
"""
 @Time : 2021/1/10 15:45
 @File : 图片换底色.py
 @Software: PyCharm
"""
import requests
from django.conf import settings

#bg_color:证件照底色蓝底：#438EDB ,红红底：#FF0000 ,白底：#FFFFFF

def change_img(img_url,bg_color,name):
    response = requests.post(
        'https://api.remove.bg/v1.0/removebg',
        files={'image_file': open(img_url, 'rb')},
        data={'size': 'auto','bg_color': bg_color},
        headers={'X-Api-Key': 'cTXsL4mRdkf4c9iZPw5hZCXx'},
    )

    path=settings.MEDIA_ROOT

    if response.status_code == requests.codes.ok:
        with open(path+name+'.png', 'wb') as out:
            out.write(response.content)
        img_src=path+name+'.png'
        return img_src
    else:
        print("Error:", response.status_code, response.text)